<?php 
class Default_UserController extends System_Controller
{

        public function indexAction()
        {

        }

        public function createAction()
        {
                //recebe post de criar usario
                $this->reciveCreateUser();
        }

        public function loginAction()
        {       
                $form = new Reuse_Zend_Form_Login;

                $userData = $this->_request->getPost();
                if($form->isValid($userData))
                {
                        $postLogin = $this->_request->getPost();
                        $dbAdapter = Zend_Db_Table::getDefaultAdapter();

                        $authAdapter = new Zend_Auth_Adapter_DbTable;

                        $authAdapter->setTableName('user')
                        ->setIdentityColumn('name')
                        ->setCredentialColumn('password')
                        ->setIdentity($postLogin['login'])
                        ->setCredential($postLogin['password']);
                        //->setCredentialTreatment("MD5(?)");

                        $auth = Zend_Auth::getInstance();
                        $result = $auth->authenticate($authAdapter);

                        if($result->isValid())
                        {   
                                $data = $authAdapter->getResultRowObject(null,$postLogin['password']);
                                $auth->getStorage()->write($data);

                                echo 'está autenticado';

                                $auth = Zend_Auth::getInstance();
                                $data = $authAdapter->getResultRowObject(null,'senha');
                                $auth->getStorage()->write($data);

                                header('Location: /projects/index');
                                return true;
                        }
                }

                $this->view->error = 'Usuário ou senha incorretos.' ;
        }

        public function logoffAction()
        {
                $auth = Zend_Auth::getInstance();
                if($auth->hasIdentity()) {
                        $auth->clearIdentity();
                        header('Location: /index/index');
                } else {
                         header('Location: /user/login');
                }
        }

        private function reciveCreateUser()
        {
                $form = new Reuse_Zend_Form_CreateUser;

                if($this->_request->isPost()) {
                        $userData = $this->_request->getPost();
                        if($form->isValid($userData))
                        {
                                $user = new User();
                                $user->insertUser($userData['name'],$userData['password'],$userData['profession'],$userData['email'],$userData['tel']);
                                $this->view->message = 'Usu&aacute;rio cadastrado!';
                        }
                        else
                        {
                                $this->view->error = 'Algum Campo est&aacute; incorreto.' ;
                        }
                }
        }
}
